package com.stylefeng.guns.gateway.modular.film;

import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.rpc.RpcContext;
import com.stylefeng.guns.api.film.FilmAsyncServiceApi;
import com.stylefeng.guns.api.film.FilmServiceApi;
import com.stylefeng.guns.api.film.vo.*;
import com.stylefeng.guns.gateway.modular.film.vo.FilmConditionVO;
import com.stylefeng.guns.gateway.modular.film.vo.FilmIndexVO;
import com.stylefeng.guns.gateway.modular.film.vo.FilmRequestVO;
import com.stylefeng.guns.gateway.modular.vo.ResponseVO;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

/**
 * @author dengwubo
 */
@RestController
@RequestMapping("/film/")
public class FilmController {
    @Reference
    private FilmServiceApi filmServiceApi;

    @Reference(async = true)
    private FilmAsyncServiceApi filmAsyncServiceApi;

    private static final String IMG_PRE = "http://img.meetingshop.cn/";

    /**
     * 获取首页信息接口
     *
     * @return
     */
    @RequestMapping("getIndex")
    public ResponseVO getIndex() {
        FilmIndexVO filmIndexVO = new FilmIndexVO();

        // 获取banner信息
        filmIndexVO.setBanners(filmServiceApi.getBanners());
        // 获取正在热映的电影
        filmIndexVO.setHostFilms(filmServiceApi.getHotFilms(true, 8, 1, 1, 99, 99, 99));
        // 获取即将上映的电影
        filmIndexVO.setSoonFilms(filmServiceApi.getSoonFilms(true, 8, 1, 1, 99, 99, 99));
        // 票房排行榜
        filmIndexVO.setBoxRanking(filmServiceApi.getBoxRanking());
        // 获取受欢迎的榜单
        filmIndexVO.setExpectRanking(filmServiceApi.getExpectRanking());
        // 获取top100
        filmIndexVO.setTop100(filmServiceApi.getTop());

        return ResponseVO.success(filmIndexVO, IMG_PRE);
    }

    /**
     * 影片条件列表查询
     *
     * @param catId
     * @param sourceId
     * @param yearId
     * @return
     */
    @RequestMapping("getConditionList")
    public ResponseVO getConditionList(
            @RequestParam(name = "catId", required = false, defaultValue = "99") String catId,
            @RequestParam(name = "sourceId", required = false, defaultValue = "99") String sourceId,
            @RequestParam(name = "yearId", required = false, defaultValue = "99") String yearId
    ) {
        FilmConditionVO result = new FilmConditionVO();
        boolean flag = false;
        // 类型集合
        List<CatVO> cats = filmServiceApi.getCats();
        List<CatVO> catResult = new ArrayList<>();
        CatVO cat = null;
        for (CatVO catVO : cats) {
            if (StrUtil.equals("99", catVO.getCatId())) {
                cat = catVO;
                continue;
            }
            if (StrUtil.equals(catId, catVO.getCatId())) {
                flag = true;
                catVO.setActive(true);
            }
            catResult.add(catVO);
        }
        if (!flag) {
            cat.setActive(true);
        }
        catResult.add(cat);

        // 片源集合
        flag = false;
        List<SourceVO> sources = filmServiceApi.getSources();
        List<SourceVO> sourceResult = new ArrayList<>();
        SourceVO source = null;
        for (SourceVO sourceVO : sources) {
            if (StrUtil.equals("99", sourceVO.getSourceId())) {
                source = sourceVO;
                continue;
            }
            if (StrUtil.equals(sourceId, sourceVO.getSourceId())) {
                flag = true;
                sourceVO.setActive(true);
            }
            sourceResult.add(sourceVO);
        }
        if (!flag) {
            source.setActive(true);
        }
        sourceResult.add(source);
        // 年代集合
        flag = false;
        List<YearVO> years = filmServiceApi.getYears();
        List<YearVO> yearResult = new ArrayList<>();
        YearVO year = null;
        for (YearVO yearVO : years) {
            if (StrUtil.equals("99", yearVO.getYearId())) {
                year = yearVO;
                continue;
            }
            if (StrUtil.equals(yearId, yearVO.getYearId())) {
                flag = true;
                yearVO.setActive(true);
            }
            yearResult.add(yearVO);
        }
        if (!flag) {
            year.setActive(true);
        }
        yearResult.add(year);
        result.setCatInfo(catResult);
        result.setSourceInfo(sourceResult);
        result.setYearInfo(yearResult);
        return ResponseVO.success(result);
    }


    /**
     * 影片查询功能
     *
     * @param filmRequestVO
     * @return
     */
    @RequestMapping("getFilms")
    public ResponseVO getFilms(FilmRequestVO filmRequestVO) {

        FilmVO filmVO = null;
        switch (filmRequestVO.getShowType()) {
            case 1:
                filmVO = filmServiceApi.getHotFilms(false, filmRequestVO.getPageSize(), filmRequestVO.getNowPage(),
                        filmRequestVO.getSortId(), filmRequestVO.getSourceId(), filmRequestVO.getYearId(), filmRequestVO.getCatId());
                break;
            case 2:
                filmVO = filmServiceApi.getSoonFilms(false, filmRequestVO.getPageSize(), filmRequestVO.getNowPage(),
                        filmRequestVO.getSortId(), filmRequestVO.getSourceId(), filmRequestVO.getYearId(), filmRequestVO.getCatId());
                break;
            case 3:
                filmVO = filmServiceApi.getClassicFilms(filmRequestVO.getPageSize(), filmRequestVO.getNowPage(),
                        filmRequestVO.getSortId(), filmRequestVO.getSourceId(), filmRequestVO.getYearId(), filmRequestVO.getCatId());
                break;
            default:
                filmVO = filmServiceApi.getHotFilms(false, filmRequestVO.getPageSize(), filmRequestVO.getNowPage(),
                        filmRequestVO.getSortId(), filmRequestVO.getSourceId(), filmRequestVO.getYearId(), filmRequestVO.getCatId());
                break;
        }

        return ResponseVO.success(filmVO.getFilmInfos(), IMG_PRE, filmVO.getNowPage(), filmVO.getTotalPage());
    }

    @RequestMapping("films/{searchParam}")
    public ResponseVO getFilms(@PathVariable("searchParam") String searchParam, int searchType) throws Exception {
        FilmDetailVO filmDetail = filmServiceApi.getFilmDetail(searchType, searchParam);
        if (filmDetail==null||StrUtil.isBlank(filmDetail.getFilmId())||StrUtil.isBlank(filmDetail.getFilmName())){
            return ResponseVO.serviceFail("没有可查询的影片");
        }
        String filmId = filmDetail.getFilmId();

//        FilmDescVO filmDescVO = filmAsyncServiceApi.getFilmDesc(filmId);
        filmAsyncServiceApi.getFilmDesc(filmId);
        Future<FilmDescVO> filmDescVOFuture = RpcContext.getContext().getFuture();

//        ImgVO imgs = filmAsyncServiceApi.getImgs(filmId);
        filmAsyncServiceApi.getImgs(filmId);
        Future<ImgVO> imgVOFuture = RpcContext.getContext().getFuture();

//        ActorVO directorInfo = filmAsyncServiceApi.getDirectorInfo(filmId);
        filmAsyncServiceApi.getDirectorInfo(filmId);
        Future<ActorVO> directorInfoFuture =  RpcContext.getContext().getFuture();

//        List<ActorVO> actors = filmAsyncServiceApi.getActors(filmId);
        filmAsyncServiceApi.getActors(filmId);
        Future<List<ActorVO>> actorsFuture = RpcContext.getContext().getFuture();
        InfoRequestVO infoRequestVO = new InfoRequestVO();
        ActorRequestVO actorRequestVO = new ActorRequestVO();

        actorRequestVO.setActors(actorsFuture.get());
        actorRequestVO.setDirector(directorInfoFuture.get());

        infoRequestVO.setActors(actorRequestVO);
        infoRequestVO.setBiography(filmDescVOFuture.get().getBiography());
        infoRequestVO.setFilmId(filmId);
        infoRequestVO.setImgs(imgVOFuture.get());

        filmDetail.setInfo04(infoRequestVO);


        return ResponseVO.success(filmDetail, IMG_PRE);
    }


}
